//
//  FKChooseConsumeDateVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/5/21.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKChooseConsumeDateVC.h"
#import "FKChooseConsumeDateCell.h"
#import "FKOrderGoods.h"
#import "FKCommitConsumeVC.h"
static NSString *const ID = @"FKChooseConsumeDateCell";

@interface FKChooseConsumeDateVC ()<UICollectionViewDataSource, UICollectionViewDelegate> {
    BOOL isShowMore;
}

@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) NSMutableDictionary *param;
@property(nonatomic, assign) NSInteger page;

@end

@implementation FKChooseConsumeDateVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
        _param[@"pagesize"] = @(10);
    }
    return _param;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    isShowMore = false;
    self.title = @"选择预约日期";
    self.param[@"order_goods_id"] = self.goods.id;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat itemW = fk_adjustW(140);
    CGFloat itemH = fk_adjustW(46);
    layout.minimumInteritemSpacing = fk_adjustW(35);
    layout.minimumLineSpacing = fk_adjustW(20);
    layout.itemSize = CGSizeMake(itemW, itemH);
    layout.headerReferenceSize = CGSizeMake(fkScreenW, 20);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    CGFloat margin = fk_adjustW(30);
    collectionView.allowsMultipleSelection = false;
    collectionView.contentInset = UIEdgeInsetsMake(0, margin, 0, margin);
    
    [collectionView registerClass:[FKChooseConsumeDateCell class] forCellWithReuseIdentifier:ID];
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.collectionView.mj_header beginRefreshing];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return isShowMore ? self.datas.count : self.datas.count ? 30 : 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKChooseConsumeDateCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.data = self.datas[indexPath.item];
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *data = self.datas[indexPath.row];
    NSInteger reserve_nums = [data[@"reserve_nums"] integerValue];
    if (reserve_nums > 0) {
        FKCommitConsumeVC *vc = [[FKCommitConsumeVC alloc] init];
        vc.goods = self.goods;
        vc.reserve_time = data[@"end_times"];
        vc.appDate = data[@"reserve_time"];
        vc.needRemoveBeforeVc = true;
        fkPushNav(vc);
    } else {
        [SVProgressHUD showInfoWithStatus:@"没有可用名额"];
        [collectionView deselectItemAtIndexPath:indexPath animated:false];
    }
}


#pragma mark - 网络请求
- (void)loadData
{
    isShowMore = false;
    self.page = 1;
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:@"m=Home&c=OrderList&a=TimeOfAppointment" param:self.param success:^(FKBaseResult *result) {
        [self.collectionView.mj_header endRefreshing];
        if (result.isSuccess) {
            self.datas = result.data[@"info"];
            [self.collectionView reloadData];
            if (self.datas.count > 30) {
//                self.page++
                self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
            }
        }
    } failure:^(NSError *error) {
        [self.collectionView.mj_header endRefreshing];
    }];
}

- (void)loadMoreData
{
//    self.param[@"page"] = @(self.page);
//    [FKHttpTool postWithSuffix:@"m=Home&c=OrderList&a=TimeOfAppointment" param:self.param success:^(FKBaseResult *result) {
//        [self.collectionView.mj_footer endRefreshing];
//        if (result.isSuccess) {
//            NSArray *newArray = result.data[@"info"];
//            if (newArray.count > 0) {
//                NSMutableArray *array = [NSMutableArray arrayWithArray:self.datas];
//                [array addObjectsFromArray:newArray];
//                self.datas = array;
//                [self.collectionView reloadData];
//                self.page++;
//            } else {
//                [self.collectionView.mj_footer endRefreshingWithNoMoreData];
//            }
//        }
//    } failure:^(NSError *error) {
//        [self.collectionView.mj_footer endRefreshing];
//    }];
    isShowMore = true;
    [self.collectionView reloadData];
    self.collectionView.mj_footer = nil;

}


@end
